<?php
require dirname(__FILE__).'/core/fanwe.php';
$fanwe = &FanweService::instance();
$fanwe->initialize();

$index = intval($_REQUEST['index']);
@set_time_limit(0);
if(function_exists('ini_set'))
    ini_set('max_execution_time',0);

$limit = 500;
$list = FDB::fetchAll('SELECT * FROM '.FDB::table('user').' ORDER BY uid DESC LIMIT '.$index.','.$limit);

if(count($list) == 0)
{
	FDB::query('ALTER TABLE '.FDB::table('user').' DROP INDEX `taobao_id`');
	FDB::query('ALTER TABLE '.FDB::table('user').' DROP INDEX `tqq_id`');
	FDB::query('ALTER TABLE '.FDB::table('user').' DROP INDEX `sina_id`');
	FDB::query('ALTER TABLE '.FDB::table('user').' DROP INDEX `qq_id`');
	FDB::query('ALTER TABLE '.FDB::table('user').' DROP COLUMN `sina_id`');
	FDB::query('ALTER TABLE '.FDB::table('user').' DROP COLUMN `sina_app_key`');
	FDB::query('ALTER TABLE '.FDB::table('user').' DROP COLUMN `sina_app_secret`');
	FDB::query('ALTER TABLE '.FDB::table('user').' DROP COLUMN `sina_syn_weibo`');
	FDB::query('ALTER TABLE '.FDB::table('user').' DROP COLUMN `sina_syn_topic`');
	FDB::query('ALTER TABLE '.FDB::table('user').' DROP COLUMN `tqq_id`');
	FDB::query('ALTER TABLE '.FDB::table('user').' DROP COLUMN `tqq_app_key`');
	FDB::query('ALTER TABLE '.FDB::table('user').' DROP COLUMN `tqq_app_secret`');
	FDB::query('ALTER TABLE '.FDB::table('user').' DROP COLUMN `tqq_syn_weibo`');
	FDB::query('ALTER TABLE '.FDB::table('user').' DROP COLUMN `tqq_syn_topic`');
	FDB::query('ALTER TABLE '.FDB::table('user').' DROP COLUMN `qq_id`');
	FDB::query('ALTER TABLE '.FDB::table('user').' DROP COLUMN `taobao_id`');
	echo "<h1>更新会员信息完成</h1>";
	exit;
}

foreach($list as $data)
{
    $bind = array();
	$bind['uid'] = $data['uid'];

	if(!empty($data['sina_id']) && !empty($data['sina_app_key']) && !empty($data['sina_app_secret']))
	{
		$bind['type'] = 'sina';
		$bind['keyid'] = $data['sina_id'];
		$bind['refresh_time'] = 0;

		$info = array();
		$info['oauth_token'] = $data['sina_app_key'];
		$info['oauth_token_secret'] = $data['sina_app_secret'];
		$bind['info'] = serialize($info);

		$sync = array();
		$sync['weibo'] = $data['sina_syn_weibo'];
		$sync['topic'] = $data['sina_syn_topic'];
		$sync['medal'] = 1;
		$bind['sync'] = serialize($sync);
		FDB::insert('user_bind',$bind,false,true);
	}

	if(!empty($data['tqq_id']) && !empty($data['tqq_app_key']) && !empty($data['tqq_app_secret']))
	{
		$bind['type'] = 'tqq';
		$bind['keyid'] = $data['tqq_id'];
		$bind['refresh_time'] = 0;

		$info = array();
		$info['access_token'] = $data['sina_app_key'];
		$info['oauth_token_secret'] = $data['sina_app_secret'];
		$bind['info'] = serialize($info);

		$sync = array();
		$sync['weibo'] = $data['tqq_syn_weibo'];
		$sync['topic'] = $data['tqq_syn_topic'];
		$sync['medal'] = 1;
		$bind['sync'] = serialize($sync);
		FDB::insert('user_bind',$bind,false,true);
	}

	if(!empty($data['qq_id']))
	{
		$bind['type'] = 'qq';
		$bind['keyid'] = $data['qq_id'];
		$bind['refresh_time'] = 0;

		$info = array();
		$info['access_token'] = '';
		$bind['info'] = serialize($info);

		$sync = array();
		$sync['weibo'] = 0;
		$sync['topic'] = 0;
		$sync['medal'] = 0;
		$bind['sync'] = serialize($sync);
		FDB::insert('user_bind',$bind,false,true);
	}
	
	if(FS("User")->getIsAvatar($data['uid']))
        FDB::query("UPDATE ".FDB::table('user')." SET avatar_status = 1 WHERE uid='".$data['uid']."'");

    echo "更新会员 $data[user_name] 成功<br/>";
	flush();
	ob_flush();
    usleep(100);
}

echo "<script type=\"text/javascript\">var fun = function(){location.href='update_user.php?index=".($index + $limit)."&time=".time()."';}; setTimeout(fun,500);</script>"."\r\n";
flush();
ob_flush();
exit;
?>